#include "OutputPicture.h"
#include "./ui_OutputPicture.h"
#include "qcustomplot.h"
#include <QSpinBox>
#include <QFileDialog>
OutputPicture::OutputPicture(QCustomPlot *plot, QWidget *parent, Qt::WindowFlags f)
    : QDialog(parent, f), plot(plot)
{
    ui = new Ui::OutputPicture;
    ui->setupUi(this);
    mPixmap = plot->toPixmap();
    ui->comboBoxResolutionUnit->addItem("像素每米", QCP::ResolutionUnit::ruDotsPerMeter);
    ui->comboBoxResolutionUnit->addItem("像素每厘米", QCP::ResolutionUnit::ruDotsPerCentimeter);
    ui->comboBoxResolutionUnit->addItem("像素每英寸", QCP::ResolutionUnit::ruDotsPerInch);
    ui->spinBoxQuality->setRange(-1, 100);
    ui->labelPreviewPicture->setPixmap(mPixmap);
    ui->spinBoxWidth->setValue(mPixmap.width());
    ui->spinBoxHeight->setValue(mPixmap.height());
    ui->doubleSpinBoxScale->setValue(1.0);
    ui->spinBoxQuality->setValue(-1);
    ui->spinBoxResolution->setValue(96);
    ui->comboBoxResolutionUnit->setCurrentIndex(2);
    connect(this, &OutputPicture::accepted, this, &OutputPicture::on_accepted);
}

bool OutputPicture::on_accepted()
{
    QString format = ui->comboBoxFormat->currentText();
    auto fileName = QFileDialog::getSaveFileName(this, tr("保存图片"), QDir::currentPath(), tr("图片 (*.%1)").arg(format));
    if (fileName.isEmpty())
    {
        return false;
    }
    if (format == "pdf")
    {
        return plot->savePdf(fileName,
                             ui->spinBoxWidth->value(),
                             ui->spinBoxHeight->value());
    }
    else if (format == "png")
    {
        return plot->savePng(fileName,
                             ui->spinBoxWidth->value(),
                             ui->spinBoxHeight->value(),
                             ui->doubleSpinBoxScale->value(),
                             ui->spinBoxQuality->value(),
                             ui->spinBoxResolution->value(),
                             ui->comboBoxResolutionUnit->currentData().value<QCP::ResolutionUnit>());
    }
    else if (format == "jpg")
    {
        return plot->saveJpg(fileName,
                             ui->spinBoxWidth->value(),
                             ui->spinBoxHeight->value(),
                             ui->doubleSpinBoxScale->value(),
                             ui->spinBoxQuality->value(),
                             ui->spinBoxResolution->value(),
                             ui->comboBoxResolutionUnit->currentData().value<QCP::ResolutionUnit>());
    }
    else if (format == "bmp")
    {
        return plot->saveBmp(fileName,
                             ui->spinBoxWidth->value(),
                             ui->spinBoxHeight->value(),
                             ui->doubleSpinBoxScale->value(),
                             ui->spinBoxResolution->value(),
                             ui->comboBoxResolutionUnit->currentData().value<QCP::ResolutionUnit>());
    }
    return false;
}

void OutputPicture::on_rejected()
{
}

void OutputPicture::on_comboBoxFormat_currentIndexChanged(int index)
{
    QString format = ui->comboBoxFormat->currentText();
    ui->spinBoxQuality->setEnabled(true);
    ui->spinBoxWidth->setEnabled(true);
    ui->spinBoxHeight->setEnabled(true);
    ui->doubleSpinBoxScale->setEnabled(true);
    ui->spinBoxResolution->setEnabled(true);
    ui->comboBoxResolutionUnit->setEnabled(true);
    if (format == "pdf")
    {
        ui->spinBoxQuality->setEnabled(false);
        ui->doubleSpinBoxScale->setEnabled(false);
        ui->spinBoxResolution->setEnabled(false);
        ui->comboBoxResolutionUnit->setEnabled(false);
    }
    else if (format == "png")
    {
    }
    else if (format == "jpg")
    {
    }
    else if (format == "bmp")
    {
        ui->spinBoxQuality->setEnabled(false);
    }
}

void OutputPicture::on_horizontalSlider_valueChanged(int value)
{
    ui->labelRatio->setText(QString::number(value) + "%");
    int w = mPixmap.width() * (value / 100);
    int h = mPixmap.height() * (value / 100);
    qDebug() << w <<" "<< h;
    ui->labelPreviewPicture->setPixmap(mPixmap.scaled(w, h, Qt::KeepAspectRatio));
    ui->labelPreviewPicture->update();
}
